package com.jprocms.module.cms.service.link;

import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.util.object.BeanUtils;
import com.jprocms.module.cms.controller.admin.link.vo.LinkCreateReqVO;
import com.jprocms.module.cms.controller.admin.link.vo.LinkListReqVO;
import com.jprocms.module.cms.controller.admin.link.vo.LinkPageReqVO;
import com.jprocms.module.cms.controller.admin.link.vo.LinkUpdateReqVO;
import com.jprocms.module.cms.convert.link.LinkConvert;
import com.jprocms.module.cms.dal.dataobject.link.LinkDO;
import com.jprocms.module.cms.dal.mysql.link.LinkMapper;
import com.jprocms.module.cms.util.CmsContextUtil;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.service.impl.DiffParseFunction;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.LINK_NOT_EXISTS;
import static com.jprocms.module.cms.enums.LogRecordConstants.*;

/**
 * 友情链接 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class LinkServiceImpl implements LinkService {

    @Resource
    private LinkMapper linkMapper;

    @Override
    @LogRecord(type = CMS_LINK_TYPE, subType = CMS_LINK_CREATE_SUB_TYPE, bizNo = "{{#link.id}}", success = CMS_LINK_CREATE_SUCCESS)
    public Long createLink(LinkCreateReqVO createReqVO) {
        createReqVO.setSiteId(CmsContextUtil.getCurrSiteId());
        // 插入
        LinkDO link = LinkConvert.INSTANCE.convert(createReqVO);
        linkMapper.insert(link);
        // 记录操作日志上下文
        LogRecordContext.putVariable("link", link);
        // 返回
        return link.getId();
    }

    @Override
    @LogRecord(type = CMS_LINK_TYPE, subType = CMS_LINK_UPDATE_SUB_TYPE, bizNo = "{{#updateReqVO.id}}", success = CMS_LINK_UPDATE_SUCCESS)
    public void updateLink(LinkUpdateReqVO updateReqVO) {
        // 校验存在
        LinkDO oldLink = validateLinkExists(updateReqVO.getId());
        // 更新
        LinkDO updateObj = LinkConvert.INSTANCE.convert(updateReqVO);
        linkMapper.updateById(updateObj);
        // 记录操作日志上下文
        LogRecordContext.putVariable(DiffParseFunction.OLD_OBJECT, BeanUtils.toBean(oldLink, LinkUpdateReqVO.class));
        LogRecordContext.putVariable("link", updateObj);
    }

    @Override
    @LogRecord(type = CMS_LINK_TYPE, subType = CMS_LINK_DELETE_SUB_TYPE, bizNo = "{{#id}}", success = CMS_LINK_DELETE_SUCCESS)
    public void deleteLink(Long id) {
        // 校验存在
        LinkDO linkDO = validateLinkExists(id);
        // 删除
        linkMapper.deleteById(id);
        // 记录操作日志上下文
        LogRecordContext.putVariable("link", linkDO);
    }

    private LinkDO validateLinkExists(Long id) {
        final LinkDO linkDO = linkMapper.selectById(id);
        if (linkDO == null) {
            throw exception(LINK_NOT_EXISTS);
        }
        return linkDO;
    }

    @Override
    public LinkDO getLink(Long id) {
        return linkMapper.selectById(id);
    }

    @Override
    public List<LinkDO> getLinkList(Collection<Long> ids) {
        return linkMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LinkDO> getLinkPage(LinkPageReqVO pageReqVO) {
        return linkMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LinkDO> getLinkList(LinkListReqVO exportReqVO) {
        return linkMapper.selectList(exportReqVO);
    }

}
